// ApplicationsManager.cpp : implementation of the CApplicationsManager class
//
#include "stdafx.h"
#include "ApplicationAccessManager.h"
#include "AccountDlg.h"
#include "WinField.h"




static UINT m_nHCBT_ACTIVATE=::RegisterWindowMessage(_T("OPENESSO_HOOK_HCBT_ACTIVATE"));

BEGIN_MESSAGE_MAP(CApplicationAccessManager,CApplicationAccessManager)
	ON_COMMAND(ID_SSO_START, &CApplicationAccessManager::OnSsoStart)
	ON_COMMAND(ID_SSO_STOP, &CApplicationAccessManager::OnSsoStop)
	ON_REGISTERED_THREAD_MESSAGE(m_nHCBT_ACTIVATE,&CApplicationAccessManager::OnRegisteredMessage)
END_MESSAGE_MAP()

CApplicationAccessManager::CApplicationAccessManager()
{
	// TODO: add member initialization code here
	appMgr=CApplicationsManager().getInstance();
}
CApplicationAccessManager::~CApplicationAccessManager()
{
	
}

// COpenESSOApp message handlers

void CApplicationAccessManager::OnRegisteredMessage(WPARAM wp,LPARAM lp)
{
	TCHAR szBuf1[256];
	GetWindowText((HWND)wp,szBuf1,256);
	CString str=szBuf1;
	MessageBox(0,_T("lala"),_T("jj"),MB_HELP);
}

//analyse application and it's fields from windows title
void CApplicationAccessManager::ApplicationAnalyser(CString windowTitle)
{
	GListIterator itWinApp(appMgr->GetWinAppList());
	while(itWinApp())
	{
		CWinApplication* winAp=(CWinApplication *)itWinApp++;
		GListIterator itWinForm(winAp->GetWinFormList());
		while(itWinForm())
		{
			CWinForm *pWinForm = (CWinForm *)itWinForm++;
			GListIterator itWinTitle(pWinForm->GetWindowTitleList());
			while(itWinTitle())
			{
				CWindowTitle *pWinTitle = (CWindowTitle *)itWinTitle++;
				if(pWinTitle->GetValue().Compare((LPCTSTR)windowTitle)==0)
				{
					//start winform option analysis


					//start field analysis
					GListIterator itWinField(pWinForm->GetWinFieldList());
					while(itWinField())
					{
						CWinField *pWinField= (CWinField *)itWinField++;
						pWinField->GetControlID();
						pWinField->GetControlClass();
					
					}





					/*CAccountDlg* accountDlg=new CAccountDlg() ;
					GList* accounts=appMgr->GetAccountListByAppName((LPCTSTR)winAp->GetName());
					GListIterator itAcc(accounts);
					while(itAcc())
					{
						CAccount *acc=(CAccount *)itAcc++;
						accountDlg->Create(IDD_LOGONBOX);
						accountDlg->SetAccount(*acc);
						accountDlg->SetWinApplication(*winAp);
						accountDlg->UpdateData();
						accountDlg->ShowWindow(SW_SHOW);
					}*/
				}
			}
		}
	}
}



void CApplicationAccessManager::OnSsoStart()
{
	InitializeCbtHook(0, theApp.m_nThreadID);
}

void CApplicationAccessManager::OnSsoStop()
{
	UninitializeCbtHook();
}